TARBALL_DIR_NAME = "TarballDirectory"
VERSION_DIR = "Versions"

VERSION_BODY = """# NOTE!
# This file is used to determine what version of the different types of
# patches you have on your computer. Since this will be different for everyone,
# and is used to determine what patches you need, do NOT add this to the SVN
# directory.

iCurrentVersion = %d
"""

import os
import shutil

def getPatchTypes():
    return ["DLL", "Media", "Other", "Exit"]


def checkExistingDirectory():
    if os.path.exists(TARBALL_DIR_NAME):
        shutil.rmtree(TARBALL_DIR_NAME)

def getInputs():
    pPatchTypes = getPatchTypes()

    print "Type of patch?"
    iNum = 0
    for szType in pPatchTypes:
        print "%d.) %s" % (iNum + 1, szType)
        iNum += 1

    while True:
        try:
            iInput = int(raw_input("\nEnter type: (1 - %d): " % (iNum,)))
            if iInput in range(1, iNum + 1):
                break
        except ValueError:
            pass
            

    if pPatchTypes[iInput - 1] == "Exit":
        return (iInput - 1, 0)
        
    while True:
        try:
            iVersion = int(raw_input("\nEnter version: "))
            if iVersion > 0:
                break
        except ValueError:
            pass

    return iInput - 1, iVersion
        

def createDir():
    os.mkdir(TARBALL_DIR_NAME)
    os.mkdir(os.path.join(TARBALL_DIR_NAME, VERSION_DIR))

def getVersionFilename(szVersionType):
    return "Current%sVersion.py" % (szVersionType,)

def getVersionBody(szVerisionType, iVersion):
    return VERSION_BODY % iVersion

def createVersionFile(szPatchType, iVersion):
    pFile = open(os.path.join(TARBALL_DIR_NAME, VERSION_DIR, getVersionFilename(szPatchType)), "w")
    pFile.write(getVersionBody(szPatchType, iVersion))
    pFile.close()

def run():
    pPatchTypes = getPatchTypes()

    checkExistingDirectory()
    iPatchType, iVersionNum = getInputs()
    if pPatchTypes[iPatchType] == "Exit":
        return
    createDir()
    createVersionFile(pPatchTypes[iPatchType], iVersionNum)

if __name__ == "__main__":
    run()
    print "\n"
    raw_input("Finished script. Press enter to exit")
